<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="ddh">
        <el-input
          v-model="queryParams.ddh"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="ddzt">
        <el-input
          v-model="queryParams.ddzt"
          placeholder="请输入订单状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="spmc">
        <el-input
          v-model="queryParams.spmc"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校名称" prop="xxmc">
        <el-input
          v-model="queryParams.xxmc"
          placeholder="请输入学校名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号名称" prop="zhmc">
        <el-input
          v-model="queryParams.zhmc"
          placeholder="请输入账号名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="cjsj">
        <el-date-picker clearable
          v-model="queryParams.cjsj"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['operation:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operation:order:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operation:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['operation:order:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="15" align="center" />
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="订单号" width="200" align="center" prop="ddh" />
      <el-table-column label="商品名称" align="center" prop="spmc" />
      <el-table-column label="学校名称" align="center" prop="xxmc" />
      <el-table-column label="账号名称" align="center" prop="zhmc" />
      <el-table-column label="账号密码" align="center" prop="zhmm" />
      <el-table-column label="订单状态" align="center" prop="ddzt" />
      <el-table-column label="创建时间" align="center" prop="cjsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cjsj, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['operation:order:edit']"
          >修改</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['operation:order:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="订单号" prop="ddh">
          <el-input v-model="form.ddh" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="订单状态" prop="ddzt">
          <el-input v-model="form.ddzt" placeholder="请输入订单状态" />
        </el-form-item>-->
<!--        <el-form-item label="商品名称" prop="spmc">-->
<!--          <el-select v-model="form.spmc" :optionSource="productList" placeholder="请选择商品名称" />-->
<!--        </el-form-item>-->
        <el-form-item label="商品名称" prop="spmc">
          <span style="color: red; margin-right: 10px">(必填)</span>
          <el-select
            v-model="form.spmc"
            placeholder="请选择商品名称"
            clearable
            :loading="productLoading"
            @change="handleProductChange"
          >
          <el-option
            v-for="item in productOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
          </el-select>
        </el-form-item>
        <input type="hidden" v-model="form.spid" />
        <el-form-item label="学校名称" prop="xxmc">
          <span style="color: red; margin-right: 10px">(必填)</span>
          <el-select
            v-model="form.xxmc"
            placeholder="请选择学校名称"
            clearable
            :loading="productLoading"
            @change="handleSchoolChange"
          >
            <el-option
              v-for="item in schoolOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <input type="hidden" v-model="form.xxid" />
        <el-form-item label="账号名称" prop="zhmc">
          <span style="color: red;margin-right: 10px">(必填)</span>
          <el-input v-model="form.zhmc" style="width: 250px" placeholder="请输入账号名称" />
        </el-form-item>
        <el-form-item label="账号密码" prop="zhmm">
          <span style="color: red;margin-right: 10px">(必填)</span>
          <el-input v-model="form.zhmm" style="width: 250px" placeholder="请输入账号密码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/operation/order";
import { listProduct } from "@/api/operation/product";
import { listSchool } from "@/api/operation/school";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ddh: null,
        ddzt: null,
        spmc: null,
        spid: null,
        xxmc: null,
        xxid: null,
        zhmc: null,
        zhmm: null,
        cjsj: null,
        gxsj: null,
        bz: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //商品列表
      productList: [],
      productLoading: false,
      //学校列表
      schoolList: [],
    };
  },
  //执行顺序created->computed->mounted
  created() {
    this.getList();
    this.getProductList();
    this.getSchoolList();
  },
  computed: {
    productOptions() {
      return this.productList;
    },
    schoolOptions() {
      return this.schoolList;
    }
  },
  methods: {
    /** 查询商品信息列表 */
    getProductList() {
      this.loading = true;
      listProduct(null).then(response => {
        console.log(response.rows)
        this.productList = response.rows.map(item => ({
          value: item.id,  // 使用productId作为value
          label: `${item.spmc} (¥${item.spdj})`  // 组合显示名称和价格
        }));
      });
    },
    handleProductChange(value) {
      const selected = this.productOptions.find(item => item.value === value);
      this.form.spid = selected?.value;
      this.form.spmc = selected?.label;
    },
    /** 查询学校信息列表 */
    getSchoolList() {
      this.loading = true;
      listSchool(null).then(response => {
        console.log(response.rows)
        this.schoolList = response.rows.map(item => ({
          value: item.id,  // 使用productId作为value
          label: item.xx // 组合显示名称和价格
        }));
      });
    },
    handleSchoolChange(value) {
      const selected = this.schoolOptions.find(item => item.value === value);
      this.form.xxid = selected?.value;
      this.form.xxmc = selected?.label;
    },
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ddh: null,
        ddzt: null,
        spmc: null,
        spid: null,
        xxmc: null,
        xxid: null,
        zhmc: null,
        zhmm: null,
        cjsj: null,
        gxsj: null,
        bz: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单信息编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('operation/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入"
      this.upload.open = true
    },
  }
};
</script>
